#!/usr/bin/env python

class Hero:
    # 游戏英雄
    def __init__(self, name, life, damage):
        # 游戏开发人员用的构造函数
        self.name = name
        self.life = life
        self.damage = damage
        self.life0 = life
        self.weapon = None

    @staticmethod
    def fromstr(s):
        # 用户用的构造函数
        name, life, damage = s.split()
        return Hero(name, float(life), float(damage))

    def hit(self, other):
        # self hits other
        # other.life = other.life - self.damage
        other.life -= self.damage
     
    def isdead(self):
        return self.life <= 0

    def reset(self):
        # 回血
        self.life = self.life0

import random
import time

class Army:
    '''
    Army has 1 (principal) proptery
    heros: heros
    '''
    def __init__(self, name, heros):
        self.name = name
        self.heros = heros

    @property
    def damage(self):
        # 军队的输出 = 军队中英雄的总输出
        return sum(hero.damage for hero in self.heros)

    def hit(self, other):
        # 军队self 攻击 军队other
        hero = random.choice(other.heros)
        hero.life -= self.damage
        # 军队other某个英雄首先被攻击
        if hero.isdead():
            # 死亡之后，删除
            other.heros.remove(hero)
            return hero

    def isdead(self):
        # 判断军队是否全军覆没
        return self.heros == []


class Weapon(object):
    '''Weapon has 2 (principal) propteries
    name: name
    damage: damage'''
    def __init__(self, name, damage):
        self.name = name
        self.damage = damage

    def __str__(self):
        return self.name


heros1 = [Hero.fromstr('山本五十六 200 20'), Hero.fromstr('南云忠一 50 10'), Hero.fromstr('山口多闻 20 10')]
heros2 = [Hero.fromstr('尼米兹 400 20'), Hero.fromstr('弗莱彻 100 10'), Hero.fromstr('罗斯福 30 10')]

weapons = ('鱼雷', '舰炮', '深水炸弹', '舰载机', '轰炸机', '导弹')

army1 = Army('大日本帝国太平洋联合舰队', heros1)
army2 = Army('美利坚太平洋特混舰队', heros2)


from tkinter import *           # 导入 Tkinter 库
# import tkinter

root = Tk()                     # 创建窗口对象的背景色
                                # 创建两个列表
text  = Text(root)              #  创建两个列表组件
text.pack()                     # 将小部件放置到主窗口中
text.insert(CURRENT, '第二次世界大战太平洋海战 --- 人类历史上规模最大的海战\n')

k = 0

def _next():
    global k, hero
    k+=1
    text.insert(CURRENT, '第 %d 秒：'%k)

    hero = None
    if random.random() < 0.7: # 模拟速度 0.7次/s
        hero = army1.hit(army2)
        weapon = random.choice(weapons)
        text.insert(CURRENT, '%s 用 %s 攻击 %s\n'%(army1.name, weapon, army2.name))

    if random.random() < 0.3:
        hero = army2.hit(army1)
        weapon = random.choice(weapons)
        text.insert(CURRENT, '%s 用 %s 攻击 %s\n'%(army2.name, weapon, army1.name))
    
    if hero:
        text.insert(CURRENT, '%s 阵亡\n'%hero.name)

    if army1.isdead():
        text.insert(CURRENT, '%s 全军覆没\n'%army1.name)
        text.insert(CURRENT, '%s 剩下 %d 个英雄\n'%(army2.name, len(army2.heros)))
        # button.disable()

    if army2.isdead():
        text.insert(CURRENT, '%s 全军覆没\n'%army2.name)
        text.insert(CURRENT, '%s 剩下 %d 个英雄\n'%(army1.name, len(army1.heros)))
        # button.disable()

button = Button(root, text="Next", command=_next)
button.pack(side=LEFT)

root.mainloop()
